<?php
class SystemAction extends CommonAction{
	
	public function config_sys(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统设置','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统配置','href'=>U('System:config_sys'),'separator'=>'',),
		);
		$data['action'] = U('System/config_sys',array('action'=>'submit'));		
		$Config = M('Config');
		if(!empty($_GET['action']) && $_GET['action'] == 'submit'){
			foreach($_POST['fields'] as $k => $v){
				if(get_magic_quotes_gpc()){
					$field['content'] = stripslashes($v);
				}
				$Config->where("name='$k' and ftype=1")->data($field)->save();
			}
			$this->success("修改成功！" );
			exit();
		}
		$list = $Config->where('ftype=1')->select();
		foreach($list as $v){
			$conf[$v['name']]['content'] = $v['content'];
			$conf[$v['name']]['title'] = $v['title'];
			$conf[$v['name']]['name'] = $v['name'];
			$conf[$v['name']]['discript'] = $v['discript'];
			$conf[$v['name']]['width'] = $v['width'];
			$conf[$v['name']]['type'] = $v['type'];
		}
		
		$this->assign('config',$conf);
		$this->assign('data',$data);
		$this->display();
	}
	
	public function user_sys(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统设置','href'=>'#','separator'=>'>>',),
		     array('text'=>'会员配置','href'=>U('System/user_sys'),'separator'=>'',),
		);
		$data['action'] = U('System/user_sys?action=submit');
		$Config = M('Config');
		if(!empty($_GET['action']) && $_GET['action'] == 'submit'){
			foreach($_POST['fields'] as $k => $v){
				if(get_magic_quotes_gpc()){
					$field['content'] = stripslashes($v);
				}
				$Config->where("name='$k' and ftype=5")->data($field)->save();
			}
			$this->success("修改成功！" );
			exit();
		}
		$list = $Config->where('ftype=5')->select();
		foreach($list as $v){
			$conf[$v['name']]['content'] = $v['content'];
			$conf[$v['name']]['title'] = $v['title'];
			$conf[$v['name']]['name'] = $v['name'];
			$conf[$v['name']]['discript'] = $v['discript'];
			$conf[$v['name']]['width'] = $v['width'];
			$conf[$v['name']]['type'] = $v['type'];
		}
		$this->assign('config',$conf);
		$this->assign('data',$data);
		$this->display();
	}
	
	public function email_sys(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统设置','href'=>'#','separator'=>'>>',),
		     array('text'=>'邮件配置','href'=>U('System/email_sys'),'separator'=>'',),
		);
		$data['action'] = U('System/email_sys?action=submit');
		$Config = M('Config');
		if(!empty($_GET['action']) && $_GET['action'] == 'submit'){
			foreach($_POST['fields'] as $k => $v){
				if(get_magic_quotes_gpc()){
					$field['content'] = stripslashes($v);
				}
				$Config->where('name='.$k.' and ftype=3')->data($field)->save();
			}
			$this->success("修改成功！" );
			exit();
		}
		$list = $Config->where('ftype=3')->select();
		foreach($list as $v){
			$conf[$v['name']]['content'] = $v['content'];
			$conf[$v['name']]['title'] = $v['title'];
			$conf[$v['name']]['name'] = $v['name'];
			$conf[$v['name']]['discript'] = $v['discript'];
			$conf[$v['name']]['width'] = $v['width'];
			$conf[$v['name']]['type'] = $v['type'];
		}
		$this->assign('config',$conf);
		$this->assign('data',$data);
		$this->display();
	}
	
	public function upload_sys(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统设置','href'=>'#','separator'=>'>>',),
		     array('text'=>'上传配置','href'=>U('System/upload_sys'),'separator'=>'',),
		);
		$data['action'] = U('System/upload_sys?action=submit');
		$Config = M('Config');
		if(!empty($_GET['action']) && $_GET['action'] == 'submit'){
			foreach($_POST['fields'] as $k => $v){
				if(get_magic_quotes_gpc()){
					$field['content'] = stripslashes($v);
				}
				$Config->where('name='.$k.' and ftype=4')->data($field)->save();
			}
			$this->success("修改成功！" );
			exit();
		}
		$list = $Config->where('ftype=4')->select();
		foreach($list as $v){
			$conf[$v['name']]['content'] = $v['content'];
			$conf[$v['name']]['title'] = $v['title'];
			$conf[$v['name']]['name'] = $v['name'];
			$conf[$v['name']]['discript'] = $v['discript'];
			$conf[$v['name']]['width'] = $v['width'];
			$conf[$v['name']]['type'] = $v['type'];
		}
		$this->assign('config',$conf);
		$this->assign('data',$data);
		$this->display();
	}
	
	public function add_sys(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统设置','href'=>'#','separator'=>'>>',),
		     array('text'=>'添加参数','href'=>U('System/add_sys'),'separator'=>'',),
		);
		$data['action'] = U('System/add_sys',array('action'=>'submit'));
		$Config = D('Config');
		if(!empty($_GET['action']) && $_GET['action'] == 'submit'){
			if(!$Config->create($_POST['fields'])){
				$this->error($Config->getError());	
			}else{
				$Config->add();
				$this->success("参数添加成功！");
				exit();
			}
		}
		$this->assign('data',$data);
		$this->display();
	}
	
	public function sys_log(){
		$data['breadcrumbs'] = array(
		     array('text'=>'后台首页','href'=>'#','separator'=>'>>',),
		     array('text'=>'系统日志','href'=>U('System/sys_log'),'separator'=>'',),
		);
		$data['action'] = U('System/sys_log?action=submit');
		$this->assign('data',$data);	
		$this->display();
	}
	
	public function updata(){
		$config		=	M("Config");
		$list		=	$config->getField('name,content');
		$savefile		=	DATA_PATH.'~config.php';
		// 所有配置参数统一为大写
		$content		=   "<?php\nreturn ".var_export(array_change_key_case($list,CASE_UPPER),true).";\n?>";
		if(file_put_contents($savefile,$content)){
			$this->success('配置缓存生成成功！',U('Common/welcome'));
		}else{
			$this->error('配置缓存失败！',U('Common/welcome'));
		}
	}
}
?>